<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page  import ="com.elookinto.blackhat.*, java.sql.*" %>
<% boolean isMobile = isMobile(request, session);
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Expires", -1);%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    </head>
    <body>
        <table align="center"> <tr>
                <%
                    String q = request.getParameter("q");
                    String num = request.getParameter("num");
                    String mobile = request.getParameter("mobile");
                   if ( mobile!=null) {
                       num = "1";
                       isMobile = true;
                   }
                    //create table clickbank(id identity, url varchar(100), linktext varchar(100), desctext varchar(500),  insert_time timestamp, keywords varchar(200 ));
                    Connection con = null;
                    try {
                        con = Util.getCon();
                        PreparedStatement ps = con.prepareStatement("select url, linktext, desctext from clickbank  where keywords =? ORDER BY RAND() LIMIT  " + num);
                        ps.setString(1, q);

                        ResultSet rs = ps.executeQuery();
                        //out.println("before");
                        if (!rs.next()) {
                            // out.println("after");
                            ps = con.prepareStatement("select url, linktext, desctext from clickbank   ORDER BY RAND() LIMIT  " + num);
                            rs = ps.executeQuery();
                            rs.next();
                        }
                        do {%>
                        <td> <div style="margin:0;padding: 0"> <h5 style="margin:0;padding: 0" align="center"> <a target="_blank" style="font-weight: bold;" href='<%= rs.getString("url")%>'> <%= rs.getString("linktext")%> <a> </h5>
                                    <% if (!isMobile) {%>
                                    <span style="font-size: small;text-align: center"> <%= rs.getString("desctext")%> </span>
                                    <%}%>
                                    </div> </td>
                                    <%} while (rs.next());

                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        } finally {
                                            con.close();
                                        }


                                    %>
                                    </tr>
                                    </table>
                                    </body>
                                    </html>

                                    
                                    <%!
    static boolean isMobile(HttpServletRequest request, HttpSession session) {
        if (session.getAttribute("mobile") != null) {
            return (Boolean) session.getAttribute("mobile");
        }
        String ua = request.getHeader("User-Agent").toLowerCase();
        System.out.println(ua);
        java.util.regex.Pattern p = java.util.regex.Pattern.compile(".*(iPhone|mobile|iPod|blackberry|DoCoMo|cldc|android|htc|lg|midp|mmp|mobile|nokia|opera mini|palm|pocket|psp|sgh|smartphone|symbian|treo mini|Playstation Portable|SonyEricsson|Samsung|MobileExplorer|PalmSource|Benq|Windows Phone|Windows Mobile|IEMobile|Windows CE|Nintendo Wii).*", java.util.regex.Pattern.CASE_INSENSITIVE);
        java.util.regex.Matcher matcher = p.matcher(ua);
        if (matcher.matches()) {
            session.setAttribute("mobile", true);
            return true;
        } else {
            session.setAttribute("mobile", false);
            return false;
        }
    }

    enum Browser {

        IE,
        Firefox,
        CHROME;
    };

    static Browser browser(HttpServletRequest request) {
        String ua = request.getHeader("User-Agent");

        if (ua != null && ua.indexOf("MSIE") != -1) {
            return Browser.IE;
        }

        if (ua != null && ua.indexOf("Chrome/") != -1) {
            return Browser.CHROME;
        }
        //boolean isSafari = (!isChrome && (ua != null && ua.indexOf("Safari/") != -1));

        if (ua != null && ua.indexOf("Firefox/") != -1) {
            return Browser.Firefox;
        }
        return null;

    }
%>